package com.kuafuai.api.controller;

import com.kuafuai.api.service.ApiSdkGeneratorService;
import com.kuafuai.common.domin.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/sdk")
@Slf4j
public class ApiSdkController {

    @Autowired
    private ApiSdkGeneratorService apiSdkGeneratorService;

    /**
     * 根据API Market ID生成JS SDK代码
     * @param marketId API市场ID
     * @return JS SDK代码
     */
    @GetMapping(value = "/generate/{marketId}", produces = MediaType.TEXT_PLAIN_VALUE)
    public Object generateSdk(@PathVariable Integer marketId) {
        log.info("生成SDK, marketId: {}", marketId);
        String sdkCode = apiSdkGeneratorService.generateSdkByMarketId(marketId);
        return ResultUtils.success(sdkCode);
    }

    /**
     * 根据API Market ID生成并下载JS SDK代码
     * @param marketId API市场ID
     * @return JS文件内容
     */
    @GetMapping(value = "/download/{marketId}", produces = "application/javascript")
    public String downloadSdk(@PathVariable Integer marketId) {
        log.info("下载SDK, marketId: {}", marketId);
        return apiSdkGeneratorService.generateSdkByMarketId(marketId);
    }
}
